package io.linfeng.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

/**
 * @author ：ytl
 * @date ：Created in 2021/12/30 10:47
 * @description：
 */
@Configuration
@Data
public class StaticConfig implements WebMvcConfigurer {


    @Value("${img.upload.path:./img/upload/}")
    private String uploadImgPath;



    private String uploadPath="/img/upload/";


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //addResourceHandler是指定的虚拟路径，addResourceLocations是自己的物理路径，
        registry.addResourceHandler(uploadPath+"**")
                .addResourceLocations("file:"+uploadImgPath)
                // 于设置缓存控制头（cache-control header）30天过期（30天内请求相同资源则不在发送请求）
                .setCacheControl(CacheControl.maxAge(15, TimeUnit.DAYS).cachePublic())
                .resourceChain(true);
    }
}
